package com.tutu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tutu.reggie.common.BaseContext;
import com.tutu.reggie.common.CustomException;
import com.tutu.reggie.entity.*;
import com.tutu.reggie.mapper.OrderDetailMapper;
import com.tutu.reggie.mapper.OrderMapper;
import com.tutu.reggie.mapper.ShoppingCartMapper;
import com.tutu.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author ：JK.TuTu
 * @date ：Created in 2022/9/2 16:25
 * @description：
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private UserService userService;


    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;


    /**
     * 提交订单
     * @param orders
     */
    public void submit(Orders orders) {
        //查询当前用户id
        Long currentId = BaseContext.getCurrentId();
        //根据用户id获取用户的购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        if(shoppingCarts==null||shoppingCarts.size()==0){
            throw new CustomException("购物车为空,无法下单!");
        }

        //获取订单号
        Long orderId=IdWorker.getId();

        //通过购物车数据获取orderdetail数据
        AtomicInteger amount =new AtomicInteger(0);

        List<OrderDetail> orderDatails = shoppingCarts.stream().map((item) -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setNumber(item.getNumber());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setAmount(item.getAmount());
            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());


        //查询地址数据
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if(addressBook==null){
            throw new CustomException("未查询到用户地址信息,无法下单！");
        }


        //查询用户数据
        User user = userService.getById(currentId);
        //id
        orders.setId(orderId);
        //订单号
        orders.setNumber(String.valueOf(orderId));
        //订单状态
        orders.setStatus(2);
        //下单用户id
        orders.setUserId(currentId);
        //下单时间
        orders.setOrderTime(LocalDateTime.now());
        //结账时间
        orders.setCheckoutTime(LocalDateTime.now());
        //支付方式
        orders.setPayMethod(1);
        //实收金额
        orders.setAmount(BigDecimal.valueOf(amount.get()));
        //用户名
        orders.setUserName(user.getName());
        //手机号
        orders.setPhone(user.getPhone());
        //地址
        orders.setAddress(addressBook.getDetail());
        //收货人
        orders.setConsignee(addressBook.getConsignee());
        //向order订单表中插入数据
        this.save(orders);
        //向订单详情表中插入数据
        orderDetailService.saveBatch(orderDatails);
        //清空购物车数据
        shoppingCartService.remove(queryWrapper);

    }
}
